//app.js
App({
  onLaunch: function() {

  },
  getAuthKey: function() {
    const me = this
    const userInfo = wx.getStorageSync('userInfo') || {}
    const arr = Object.keys(userInfo)
    return new Promise(function(resolve, reject) {
      if (arr.length > 0) { //本地缓存有用户信息,直接获取openId
        me.globalData.userInfo = userInfo
        me.getOpenId().then(function(res){
          if(res.status === 200){
            resolve(res)
          }else{
            reject(res)
          }
        })
      } else { //本地没有缓存用户信息
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) { //有登录授权
              wx.login({
                success: res => {
                  const code = res.code
                  wx.getUserInfo({ //从微信获取用户信息
                    success: res => {
                      const {
                        encryptedData,
                        iv,
                        signature,
                        rawData,
                        userInfo
                      } = res
                      wx.request({ //保存用户信息到服务器
                        url: me.globalData.host + 'user/store',
                        method: 'POST',
                        data: {
                          code: code,
                          encryptedData: encryptedData,
                          iv: encodeURIComponent(iv)
                        },
                        success(result) {
                          if (result.data.status === 'success') {
                            me.globalData.openId = result.data.data.open_id
                            wx.setStorage({
                              key: 'userInfo',
                              data: userInfo,
                            })
                            me.globalData.userInfo = userInfo
                            resolve({
                              status: 200,
                              msg: '用户信息处理完成'
                            })
                          }
                        }
                      })
                    }
                  })
                }
              })
            } else { //没有登录授权,只获取openId
              me.getOpenId().then(function(res) {
                if (res.status === 200) {
                  resolve(res)
                } else {
                  reject(res)
                }
              })
            }
          }
        })
      }
    })
  },
  getOpenId() { //单纯的获取openid
    const me = this;
    return new Promise(function(resolve, reject) {
      wx.login({
        success: res => {
          wx.request({
            url: me.globalData.host + 'user/getOpenId',
            method: 'POST',
            data: {
              code: res.code
            },
            success: res => {
              if (res.data.status === 'success') {
                me.globalData.openId = res.data.data.openid
                // me.connectWebSocket(res.data.data.openid)
                resolve({
                  status: 200,
                  data: '完成'
                })
              }
            },
            fail: res => {
              reject({status: 300,msg: '获取openId失败',data: res})
            }
          })
        }
      })
    })
  },
  // connectWebSocket(openid) {
    // wx.connectSocket({
    //   url: 'wss://www.yanxi.info:9502',
    //   header: {
    //     open_id: openid
    //   },
    //   complete(res) {
    //     console.log(res)
    //   }
    // })
    // wx.onSocketOpen(function(res){
    //   console.log('onSocketOpen',res)
    // })
    // wx.onSocketError(function(res){
    //   console.log('onSocketError',res)
    // })
  // },
  globalData: {
    openId: null,
    userInfo: null,
    base_host: 'https://www.yanxi.info/',
    host: 'https://www.yanxi.info/api/'
  }
})